package samsung.sds.menuManagement.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
  






import samsung.sds.menuManagement.model.MenuEntity;
import samsung.sds.menuManagement.service.MenuManagementService; 
import samsung.sds.roleManagement.model.RoleManagementEntity;
import samsung.sds.roleManagement.service.RoleManagementService;
import samsung.sds.system.common.model.QueryParam; 
import samsung.sds.system.utils.CommonTools;
import samsung.sds.system.utils.Constants;
/**
 * @author hao.wen
 * @createDate 2016-02-23
 * 
 * menuManagement的controller组件
 */
@Controller
@RequestMapping("/menuManagement")
public class MenuManagementController {

	@Autowired
	private MenuManagementService menuManagementServiceImpl;
	
	@Autowired
	private RoleManagementService roleManagementServiceImpl;
	
	/**
	 * 查询header菜单
	 */
	@RequestMapping("/loadMenu")
	@ResponseBody
	public List<MenuEntity> loadMenu(){
		QueryParam param = new QueryParam();
		return menuManagementServiceImpl.queryList(param);
	}
	
	/**
	 * 跳转到菜单管理jsp页面
	 */
	@RequestMapping(value="/menuList",method = RequestMethod.POST)
	public String menuMonitorList(){		
		return "/menuManagement/menuManagementList";
	}
	
	/**
	 * 查询菜单tree信息
	 */
	@RequestMapping("/loadMenuTree")
	@ResponseBody
	public List<MenuEntity> loadMenuTree(){
		List<MenuEntity> list = menuManagementServiceImpl.queryMenuTree(); 
		return list; 
	}
	  
	/**
	 * 将数据载入jsp页面。
	 */
	@RequestMapping(value="/menuDetails",method = RequestMethod.POST)
	@ResponseBody
	public List<MenuEntity> menuDetails(HttpServletRequest request) { 
		
		List<MenuEntity> menu = new ArrayList<MenuEntity>();
		String menuId = request.getParameter("menuId");
		menu = menuManagementServiceImpl.queryDetail(menuId); 
		
		request.setAttribute("menu", menu);
		
		
		return  menu;
	}
	
	/**
	 * 将左边角色数据载入jsp页面。
	 */
	@RequestMapping(value="/getLeftRoles",method = RequestMethod.POST)
	@ResponseBody
	public List<MenuEntity> getLeftRoles(HttpServletRequest request) {
		List<MenuEntity> roles = new ArrayList<MenuEntity>();
		String menuId = request.getParameter("menuId");
		 // 获取所有角色名称，准备发往前端
		roles = menuManagementServiceImpl.queryRoles(menuId);
		// 存入modelMap准备发往前端
		request.setAttribute("roleList", roles);
		return roles;
	}
	
	@RequestMapping(value="/getAllRoles",method = RequestMethod.POST)
	@ResponseBody
	public List<RoleManagementEntity> getAllRoles(HttpServletRequest request) {
		QueryParam param = new QueryParam();
		List<RoleManagementEntity> roleList = roleManagementServiceImpl.queryList(param); 
		// 存入modelMap准备发往前端
		request.setAttribute("roleList", roleList);
		return roleList;
	}
	
	/**
	 * 删除菜单
	 */
	@RequestMapping(value = "/deleteMenu", method = RequestMethod.POST)
	@ResponseBody
	public String deleteMenu(HttpServletRequest request) {
		String menuId = request.getParameter("menuId");
		int deleteresult = menuManagementServiceImpl.deleteMenu(menuId);
		if (deleteresult == 0) {
			return Constants.SUCCESS_CODE;
			
		} else {
			return Constants.FAILURE_CODE;
		}
	}
	
	/**
	 * 新建菜单
	 */  
	@RequestMapping(value = "/insertMenu", method = RequestMethod.POST)
	@ResponseBody
	public String insertMenu(MenuEntity menuentity) {  
		menuentity.setId(CommonTools.getKeyUUID());
		int insertresult = menuManagementServiceImpl.insertEntity(menuentity);
		if (insertresult >= 0) {
			return Constants.SUCCESS_CODE;
		} else {
			return Constants.FAILURE_CODE;
		}
	}
	
	
	/**
	 * 更新菜单
	 */  
	@RequestMapping(value = "/updateMenu", method = RequestMethod.POST)
	@ResponseBody
	public String updateMenu(MenuEntity menuentity) {  
		int updateresult = menuManagementServiceImpl.updateEntity(menuentity);
		if (updateresult > 0) {
			return Constants.SUCCESS_CODE;
		} else {
			return Constants.FAILURE_CODE;
		}
	}
	
}
